// src/store/userStore.js
import { reactive } from "vue";
import { vToast } from "@/plugins/vToast";

// 初始用户数据
const initialState = reactive([
    { id: 1, username: "admin", name: "管理员", phone: "13800138000", role: "admin", status: 1, createTime: 1718825600000, points: 9999 },
    { id: 2, username: "guest001", name: "用户", phone: "13900139000", role: "customer", status: 1, createTime: 1717916800000, points: 125 },
    { id: 3, username: "guest002", name: "用户", phone: "13700137000", role: "customer", status: 1, createTime: 1717008000000, points: 88 },
    { id: 4, username: "guest003", name: "用户", phone: "13600136000", role: "customer", status: 0, createTime: 1716099200000, points: 332 },
    { id: 5, username: "guest004", name: "用户", phone: "13500135000", role: "customer", status: 1, createTime: 1715190400000, points: 122 },
    { id: 5, username: "guest005", name: "用户", phone: "13500134000", role: "customer", status: 1, createTime: 1715190300000, points: 122 },
    { id: 5, username: "guest006", name: "用户", phone: "13500133000", role: "customer", status: 1, createTime: 1715190200000, points: 212 },
    { id: 5, username: "guest007", name: "用户", phone: "13500132000", role: "customer", status: 1, createTime: 1715190100000, points: 342 },
    { id: 5, username: "guest008", name: "用户", phone: "13500131000", role: "customer", status: 1, createTime: 1715190000000, points: 562 },
]);

// 角色映射
export const roleMapping = {
    admin: "管理员",
    sales: "销售专员",
    customer_service: "客服人员",
    operations: "运维人员",
    finance: "财务人员"
};

// 获取用户列表
export const getUsers = () => {
    return [...initialState].sort((a, b) => b.createTime - a.createTime);
};

// 添加用户
export const addUser = (newUser) => {
    const id = Math.max(...initialState.map(u => u.id)) + 1;
    const user = {
        ...newUser,
        id,
        createTime: new Date().getTime()
    };
    initialState.unshift(user);
    vToast({ message: "用户添加成功" });
    return user;
};

// 更新用户
export const updateUser = (updatedUser) => {
    const index = initialState.findIndex(u => u.id === updatedUser.id);
    if (index !== -1) {
        initialState[index] = { ...initialState[index], ...updatedUser };
        vToast({ message: "用户信息已更新" });
        return true;
    }
    return false;
};

// 删除用户
export const deleteUser = (id) => {
    const index = initialState.findIndex(u => u.id === id);
    if (index !== -1) {
        initialState.splice(index, 1);
        vToast({ message: "用户删除成功" });
        return true;
    }
    return false;
};

// 切换用户状态
export const toggleUserStatus = (id) => {
    const user = initialState.find(u => u.id === id);
    if (user) {
        user.status = user.status ? 0 : 1;
        vToast({
            message: user.status ? "用户已启用" : "用户已禁用",
            position: "top"
        });
        return true;
    }
    return false;
};

// 获取用户详情
export const getUserById = (id) => {
    const user = initialState.find(u => u.id === Number(id));
    return user ? { ...user } : null;
};